﻿using _51_AOP.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace _51_AOP.Utility.Filter
{
    public class CustomResultFilterAttribute : Attribute, IResultFilter
    {
        /// <summary>
        /// 在结果之后
        /// </summary>
        /// <param name="context"></param>
        void IResultFilter.OnResultExecuted(ResultExecutedContext context)
        {
            Console.WriteLine("CustomResultFilterAttribute==>结果之后");
        }
        /// <summary>
        /// 在结果之前
        /// </summary>
        /// <param name="context"></param>
        void IResultFilter.OnResultExecuting(ResultExecutingContext context)
        {
            if (context.Result is JsonResult) // 判断是否是json
            {
                JsonResult result = (JsonResult)context.Result;
                context.Result = new JsonResult(new AjaxResult()
                {
                    Success = true,
                    Message = "ok",
                    Data = result.Value
                }); 
            }

            Console.WriteLine("CustomResultFilterAttribute==>结果之前");  
        }
    }
}
